<!-- src/components/AdminDashboard.vue -->
<template>
  <div class="admin-dashboard">
    <div class="header">
      <div class="logo">
        <h1>管理系统</h1>
      </div>
      <div class="admin-info">
        <div class="username">{{ user.username }}</div>
        <div class="role">管理员</div>
        <button @click="logout">退出登录</button>
      </div>
    </div>
    <div class="content">
      <div class="sidebar">
        <ul>
          <li
              @click="navigateTo('UserManagement')"
              :class="{ active: currentRoute === 'UserManagement' }"
          >
            <i class="fa fa-users"></i> 用户管理
          </li>
          <li
              @click="navigateTo('PostManagement')"
              :class="{ active: currentRoute === 'PostManagement' }"
          >
            <i class="fa fa-file-text"></i> 帖子管理
          </li>
        </ul>
      </div>
      <div class="main-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user' // ✅ 引入 pinia

const router = useRouter()
const route = useRoute()
const userStore = useUserStore() // ✅ 获取 pinia 中的 user store

const user = computed(() => userStore) // ✅ user 直接来自 userStore
const currentRoute = ref('UserManagement')

onMounted(() => {
  currentRoute.value = route.name
  // ✅ 用户类型检查
  if (!userStore.isAdmin) {
  router.push({ name: 'UserDashboard' })
  } 
})

const navigateTo = (routeName) => {
  router.push({ name: routeName })
  currentRoute.value = routeName
}

const logout = () => {
  userStore.logout() // ✅ 替代 Vuex 的 dispatch
  router.push('/login')
}
</script>


<style scoped>
.admin-dashboard {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  background-color: #2c3e50;
  color: white;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.logo h1 {
  margin: 0;
  font-size: 24px;
}

.admin-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.username {
  font-weight: bold;
  font-size: 18px;
}

.role {
  background-color: #e74c3c;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 14px;
}

button {
  background-color: #34495e;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 8px 15px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #2c3e50;
}

.content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 220px;
  background-color: #f5f5f5;
  border-right: 1px solid #eee;
}

.sidebar ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.sidebar li {
  padding: 15px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background-color 0.3s;
}

.sidebar li:hover {
  background-color: #e0e0e0;
}

.sidebar li.active {
  background-color: #ddd;
  font-weight: bold;
}

.main-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #ffffff;
}
</style>